<script setup>
  import { onLoad } from '@dcloudio/uni-app'
  import { ref, computed } from 'vue'
  import { deliverApi } from '@/apis/task.js'

  // 1. 获取司机作业id
  const id = ref('')
  onLoad((params) => {
    id.value = params.id
  })

  // 2. 交付凭证数组
  const tempArr1 = ref([])
  const deliverPictureList = computed(() => {
    return tempArr1.value.map((item) => {
      return { url: item.url }
    })
  })

  // 3. 交付货品数组
  const tempArr2 = ref([])
  const certificatePictureList = computed(() => {
    return tempArr2.value.map((item) => {
      return { url: item.url }
    })
  })

  // 4. 按钮是否可用
  const submitDisabled = computed(() => {
    return (
      deliverPictureList.value.length > 0 &&
      certificatePictureList.value.length > 0
    )
  })

  // 5. 交付
  const onSubmit = async () => {
    // 表单数据
    const formData = {
      id: id.value,
      certificatePictureList: certificatePictureList.value,
      deliverPictureList: deliverPictureList.value,
    }
    // 调用接口
    const { code } = await deliverApi(formData)
    if (code !== 200) return uni.utils.toast('上传图片失败!')
    // 去到任务列表（查看在途任务）
    uni.reLaunch({ url: '/pages/task/index' })
  }
</script>

<template>
  <view class="page-container">
    <view class="receipt-info">
      <uni-file-picker
        v-model="tempArr1"
        limit="3"
        title="请拍照上传回单凭证"
        file-extname="png,jpg,gif,webp"
      ></uni-file-picker>
      <uni-file-picker
        v-model="tempArr2"
        limit="3"
        title="请拍照上传货品照片"
        file-extname="png,jpg,gif,webp"
      ></uni-file-picker>
    </view>
    <button :disabled="!submitDisabled" class="button" @click="onSubmit">
      提交
    </button>
  </view>
</template>

<style lang="scss" scoped>
  .page-container {
    padding: 30rpx;
  }

  .receipt-info {
    min-height: 600rpx;
    background-color: #fff;
    padding: 20rpx 30rpx;
    border-radius: 16rpx;

    ::v-deep .uni-file-picker {
      margin-bottom: 30rpx;
    }
  }

  .button {
    height: 100rpx;
    text-align: center;
    line-height: 100rpx;
    /* #ifdef APP */
    padding-top: 4rpx;
    /* #endif */
    border-radius: 100rpx;
    margin-top: 60rpx;
    color: #fff;
    font-size: $uni-font-size-big;
    background-color: $uni-primary;

    &[disabled] {
      color: #fff;
      background-color: #fadcd9;
    }
  }
</style>
